package ca.esdot.ane.intent;


import com.adobe.fre.FREArray;

import ca.esdot.android.ane.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class IntentActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /*
         * android.intent.action.EDIT
			android.intent.action.VIEW
			android.intent.action.SENDTO
			android.intent.action.CALL
         */
        
       String extras = "sms_body(ca.esdot.split)I am a string";  
        try {
        	//Test Extras:
        	//sendIntent(Intent.ACTION_SENDTO, "smsto:780.667.5061", null, null, null, extras);
        
        	//Test category
        	//sendIntent(Intent.ACTION_MAIN, null, null, null, "android.intent.category.HOME");
        	
        	//Test Type
        	//sendIntent(Intent.ACTION_GET_CONTENT, null, null, "vnd.android.cursor.item/phone");
        	
        	//Test Chooser + Type + Category
        	//sendIntent(Intent.ACTION_GET_CONTENT, null, "Open Something", "*/*", "android.intent.category.OPENABLE", null);
        	
        	//Test Share Image
        	//content://media/external/images/media/52
        	extras = "android.intent.extra.STREAM(ca.esdot.split)" + "file:///sdcard/DCIM/Camera/1323408120322.jpg";
        	sendIntent(Intent.ACTION_SEND, null, "Share Image", "image/jpeg", null, extras);
        	
        	//Log.v("INTENT_ACTIVITY", Uri.parse("file:///sdcard/DCIM/Camera/1323408120322.jpg") + "==" + "file:///sdcard/DCIM/Camera/1323408120322.jpg");
        	//Intent share = new Intent("android.intent.action.SEND");
        	//share.putExtra("android.intent.extra.STREAM", Uri.parse("file:///sdcard/DCIM/Camera/1323408120322.jpg"));
        	//share.setType("image/jpeg");
        	//startActivity(Intent.createChooser(share, "Share Image"));
        	
        	
        } catch(Exception e){
        	e.printStackTrace();
        	Log.v("ERROR", "INTENT FAILED");
        }
    }
    
    public void sendIntent(String action, String url, String chooserTitle, String type, String categories, String extras){
    	Activity activity = this;
		
    	Intent intent;
    	//Create the basic intent
		if(url != null){
			intent = new Intent(action, Uri.parse(url));
		} else {
			intent = new Intent(action);
		}
		//Add extra's
		if(extras != null){
			String[] split = extras.split("\\(ca.esdot.split\\)");
			for(int i = 0; i < split.length; i++){
				if(i == split.length - 1){ break; }
				
				String key = split[i];
				String value = split[++i];
				
				if(value.indexOf("(ca.esdot.int)") != -1){
					String[] split2 = value.split("\\(ca.esdot.int\\)");
					value = split2[1];
					int intVal = Integer.parseInt(value);
					intent.putExtra(key, intVal);
				} else if(value.indexOf("(ca.esdot.float)") != -1){
					String[] split2 = value.split("\\(ca.esdot.float\\)");
					value = split2[1];
					float floatVal = Float.parseFloat(value);
					intent.putExtra(key, floatVal);
				} else {
					if(value.indexOf("file://") != -1){
						intent.putExtra(key, Uri.parse(value));
					} else {
						intent.putExtra(key, value);
					}
				}
			}
		}
		
		//Add categories
		if(categories != null){
			String[] split = categories.split(":");
			for(int i = 0; i < split.length; i++){
				intent.addCategory(split[i]);
			}
		}
		
		//Set Type
		if(type != null){
			intent.setType(type);
		}
		
		if(chooserTitle == null){
			activity.startActivity(intent);
		} else {
			activity.startActivity(Intent.createChooser(intent, chooserTitle));
		}
    }
    
    @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK && requestCode == 0) {
			String result = data.getDataString();
			Toast toast = Toast.makeText(this, result, Toast.LENGTH_LONG);
			toast.show();
		}
	}
}
